<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2023/5/9
  Time: 15:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<style>
    .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="userInfo-filter">
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="nickName" id="nickName"
                           lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="gender" value="1" title="男" checked="">
                    <input type="radio" name="gender" value="0" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">生日</label>
                <div class="layui-input-inline">
                    <input type="date" name="birthday" id="birthday"
                           lay-verify="date"  placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="email" id="email"
                           lay-verify="required" placeholder="请输入邮箱" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">余额</label>
                <div class="layui-input-block">
                    <input type="number" name="money" id="money"
                           lay-verify="required" placeholder="请输入余额" value="" class="layui-input" disabled="true">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form','miniPage','util'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniPage = layui.miniPage,
            util = layui.util;
        var $ = layui.$;

        var user='${loginUser}';

        user=JSON.parse(user);
        form.val('userInfo-filter', {
            "userId":user.userId,
            "gender":user.gender,
            "nickName": user.nickName,
            "money":user.money,
            "email":user.email
        });
        $('#birthday').val(util.toDateString(user.birthday, 'yyyy-MM-dd'));
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            user.nickName=data.field.nickName;
            user.birthday=data.field.birthday;
            user.gender=data.field.gender;
            user.email=data.field.email;
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    url: "/user/updateUser",
                    data: JSON.stringify(user),
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    type: "POST",
                    success:function(res){
                        if(res.status===200){
                            layer.msg(res.message,{icon:1,time:1000});
                            layer.close(index);
                            miniPage.hashHome();
                            // parent.layer.close(parentIndex);
                        }else{
                            layer.msg(res.message,{icon:0,time:1000});
                            layer.close(index);
                            miniPage.hashHome();
                        }
                    },
                    error:function(e){
                        console.log(e,e.message);
                        alert('error');
                    }
                });
            });
            return false;
        });

    });
</script>
